var MapPoint = require("MapPoint");

cc.Class({
    extends: cc.Component,

    initMap(game){
		
		console.log(game.empty);
		
		this.colorFlag = true;
		
		var index = 0,type1=null,type2=null;
		
		for(var i=0;i<game.heightNmb-1;i++){
			
			var wn = i%2==game.mapType?game.widthNmb:game.widthNmb-1;
			
			for(var j=0;j<wn;j++){
				
				if(game.empty&&game.empty[index]){
					
				}else{
				
				var mpx,mpy;
				
				if(i%2==game.mapType){
					mpx = game.startx+j*(game.w2+game.w);
					mpy = game.starty-game.h/2*i;
				}else{
					mpx = game.startx+game.w/2+game.w2/2+j*(game.w2+game.w);
					mpy = game.starty-game.h/2*i;
				}
				
				
				var mp = new MapPoint(mpx,mpy,-1,1,game.createNode(mpx,mpy,game.mapPointFrame,game.mapScale),null,game,index);
				
				if(game.ice&&game.ice[index])
					mp.iceNode = game.createNode(mpx-1,mpy+2,game.iceFrame,game.mapScale)
				
				
				
				for(var k=0;k<game.mapPointArr.length;k++){
					var tmp = game.mapPointArr[k];
					var tmpx = tmp.x,tmpy = tmp.y;
					
					var dx = mpx-tmpx,dy= mpy-tmpy;
					
					if(dx == (game.w2+game.w)/2 && dy == -game.h/2){//dr
						tmp.mpDR = mp;
						mp.mpUL = tmp;
					}else if(dx == -(game.w2+game.w)/2 && dy == -game.h/2){
						tmp.mpDL = mp;
						mp.mpUR = tmp;
					}else if(dx == 0 && dy == -game.h){
						tmp.mpD = mp;
						mp.mpU = tmp;
					}
					
				}
				
				
				
				game.mapPointArr.push(mp);
				}
				
				index++;
	
			}
		
		}
		
		
		this.initMapPointType(game);

		this.initMapBlock(game);
		
		this.initItem(game);
		
		//this.initCreate(game);
	},
	
	initCreate(game){
		
		if(game.levelConfig.create){
			
			for(var j=0;j<game.mapPointArr.length;j++){
				var mp = game.mapPointArr[j];
				if(game.levelConfig.create[mp.no]){
					mp.createFlag=true;
				}
			}
			
		}else{
			
			for(var j=0;j<game.mapPointArr.length;j++){
				var mp = game.mapPointArr[j];
				if(mp.mpU==null){
					mp.createFlag=true;
				}
			}
			
		}
		
	},
	
	initMapBlock(game){
		
		var strarr = game.strarr;
		
		
		for(var j=0;j<game.mapPointArr.length;j++){
			var mp = game.mapPointArr[j];
			mp.blockNode = game.createNode(mp.x-2,mp.y,game.blockPointFrameArr[mp.type1][mp.type2],game.blockScale);
			mp.blockNode.zIndex = 1;
			
			if(mp.isboom()){
				game.createNode(mp.x,mp.y-game.h/2,game.signFrame,game.mapScale);
			}
		}
	},
	
	initItem(game){
		var mapConfig = game.levelConfig;
		
		var items = mapConfig.items;
		
		if(items)
		for(var i=0;i<items.length;i++){
			var item = items[i];
			var no = item.no;
			var tmp = this.getMapPointByIndex(no,game);
			tmp.resetType(item.type1,item.type2);
		}
		
	},
	
	getMapPointByIndex(index,game){
		for(var k=0;k<game.mapPointArr.length;k++){
				var tmp = game.mapPointArr[k];
				
				if(tmp.no == index){
					
					return tmp;
				}
				
		}
		return null;
	},
	
	initMapPointType(game){
		
		do{

			for(var j=0;j<game.mapPointArr.length;j++){
				var mp = game.mapPointArr[j];
				mp.type1=-1;
			}
			
			for(var j=0;j<game.mapPointArr.length;j++){
				var mp = game.mapPointArr[j];
				
				var types = mp.getNoDisapearTypes(game.shapeNmb);
				
				if(types.length<=0){
					
					console.log("has nodisappear");
					
					this.initMapPointType(game);
					
					return ;
					
				}else{
					
					mp.type1 = types[Math.floor(Math.random()*types.length)];
					//mp.type1=6;
					
				}
				
				
				
				if(game.playerType!=3&&this.colorFlag&&Math.random()>0.95){
					this.colorFlag=false;
					mp.type2=8;
				}
				
				/*
				if(j==17)
					mp.type2=7;
				if(j==20)
					mp.type2=3;*/
				
				
			}
			
		}while(!this.canMove(game))
		
		
	},
	canMove(game){
		
		//return true;
		
		
		
		var tblock,ttype1;
		
		for(var i=0;i<game.mapPointArr.length;i++){
			
			var block = game.mapPointArr[i];
			
			var group = block.getGroup();
			
			for(var j=0;j<group.length;j++){
				
				tblock = group[j];
				
				if(tblock!=null){
					ttype1 = block.type1;
					block.type1 = tblock.type1;
					tblock.type1 = ttype1;
					var findresult = block.findSame2();
					
					tblock.type1 = block.type1;
					block.type1 = ttype1;
					
					if(findresult.type>0){
						
						return true;
					}
				}
			}
			
		}
		
		return false;
		
	},
});
